package open.witype.filetiper.fragment.file.adapter;

import android.content.Context;
import android.os.Environment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;


import open.witype.filetiper.R;
import open.witype.filetiper.fragment.file.db.RemarkDBAdapter;
import open.witype.filetiper.activity.home.interf.OnItemClickListener;
import open.witype.filetiper.model.FileItemBean;
import open.witype.filetiper.model.RemarkEntity;


/**
 * Created by Typer on 2015/5/6 0006.
 * DragRecyclerView的适配器
 */
public class RecyclerViewAdapter extends RecyclerView.Adapter<FileViewHolder> {

    private List<FileItemBean> datas;

    private OnItemClickListener itemClickListener;

    private Context context;

    private String rootPath;

    public RecyclerViewAdapter(Context context) {
        this.datas = new ArrayList<>();
        this.context = context;
        rootPath = Environment.getExternalStorageDirectory().getPath();
    }

    public void setDatas(List<FileItemBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public void onBindViewHolder(final FileViewHolder holder, final int position) {
        final FileItemBean item = datas.get(position);
        holder.item_container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemClickListener != null)
                    itemClickListener.onItemClickCallback(item, position);
            }
        });
        determineShow(item,holder,position);
    }

    /**
     * 确定item项目显示的内容
     * @param item
     * @param holder
     */
    public void determineShow(FileItemBean item,FileViewHolder holder,int position){
        holder.fileName.setText(item.getAbsName());
        int viewType = getItemViewType(position);
        FileType b = FileType.values()[viewType];
        determineType(holder, b, item);
        //文件显示文件后缀和文件大小
        if (item.isFile()) {
            if (!item.getSuffix().equals(""))
                holder.fileExtra.setText(item.getSuffix() + "    ");
            holder.fileSize.setText(item.getSize());
        }
        //文件夹显示文件中文名
        else {
            determineRemark(item, holder);
        }
    }

    /**
     * 是文件夹的时候才会设置 文件夹的中文提示
     *
     * @param item
     * @param holder
     */
    public void determineRemark(FileItemBean item, FileViewHolder holder) {
        String fileTiper = item.getRemark();
        if ((fileTiper == null || fileTiper.equals("")) && !item.isSetRemark()) {
            RemarkDBAdapter dbAdapter = new RemarkDBAdapter(context);
            dbAdapter.open();
            String tmpPath = item.getPath().replace(rootPath + "/", "").toLowerCase();
            RemarkEntity entity = dbAdapter.getRemarks(tmpPath);
            dbAdapter.close();
            item.setRemark(entity.getRemark());
            fileTiper = item.getRemark();
        }
        if (fileTiper == null || fileTiper.equals("")) {
            holder.item_filelist_content_container.setVisibility(View.GONE);
        } else {
            holder.item_filelist_content_container.setVisibility(View.VISIBLE);
            holder.fileExtra.setText(fileTiper);
        }
    }

    /**
     * 根据不同的文件/文件夹类型选择显示内容
     *
     * @param holder
     * @param b      类型
     * @param item   数据内容
     */
    public void determineType(FileViewHolder holder, FileType b, FileItemBean item) {
        switch (b) {
            case FOLDER:
                holder.file_img_tip.setVisibility(View.VISIBLE);
                holder.file_img_tip.setText(item.getAbbre());
                break;
            case TXT:
                holder.item_filelist_ic.setBackgroundResource(R.mipmap.ic_list_txt);
                break;
            case UNKNOW:
            case LOG:
            case XML:
                holder.item_filelist_ic.setBackgroundResource(R.mipmap.ic_list_unknow);
                break;
            case RAR:
            case ZIP:
                holder.item_filelist_ic.setBackgroundResource(R.mipmap.ic_list_zip);
                break;
            case DOC:
                holder.item_filelist_ic.setBackgroundResource(R.mipmap.ic_list_doc);
                break;
            case XLS:
                holder.item_filelist_ic.setBackgroundResource(R.mipmap.ic_list_xls);
                break;
            case PPT:
                holder.item_filelist_ic.setBackgroundResource(R.mipmap.ic_list_ppt);
                break;
            case JPG:
            case JPEG:
            case BMP:
            case GIF:
            case PNG:

                break;
            case APK:
                holder.item_filelist_ic.setBackgroundResource(R.mipmap.ic_list_apk);
                break;
            case MP3:
            case APE:
            case FLAC:
                holder.item_filelist_ic.setBackgroundResource(R.mipmap.ic_list_music);
                break;
            case HIDDEN:

                break;
            default:

                break;
        }
    }

    @Override
    public FileViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_filelist_item, null);
        return new FileViewHolder(view);
    }

    @Override
    public int getItemViewType(int position) {
        FileItemBean file = datas.get(position);
        if (file.isFile()) {
            String fileName = file.getFileName();
            if (fileName.startsWith("."))
                return 0;
            String suffix = file.getSuffix();
            int po;
            try {
                po = FileType.fromString(suffix);
            } catch (IllegalArgumentException e) {
                return 0;
            }
            return po;
        }
        return 1;
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

}
